package com.xiaou.buss.admin.userrole.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.service.BaseServiceImpl;
import com.xiaou.buss.admin.userrole.dao.UserRoleDao;
import com.xiaou.buss.admin.userrole.entity.UserRole;
import com.xiaou.buss.admin.userrole.service.UserRoleService;
import com.xiaou.component.utils.Tools;

/**
 * 
 * <br>
 * <b>功能：</b>UserRoleService<br>
 */
@Service("userRoleService")
public class  UserRoleServiceImpl  extends BaseServiceImpl implements UserRoleService {
  Logger logger = LoggerFactory.getLogger(getClass());
	

	@Autowired
    private UserRoleDao dao;

		
	public UserRoleDao getDao() {
		return dao;
	}


	@Override
	@Transactional
	public void addUser(String roleId, String userIds) {
		String[] uids = null;;
		if(StringUtils.isNotBlank(userIds)){
			uids=userIds.split(",");
		}
		//清空该roleId下面的所有
		for (String userId : uids) {
			UserRole userRole = new UserRole();
			userRole.setRoleId(roleId);
			userRole.setUserId(userId);
			List<UserRole> userRoles = dao.findExist(userRole);
			if(userRoles.isEmpty()){//如果不存在就新增
				userRole.setId(Tools.tools.uuid());
				dao.add(userRole);
			}
		}
	}


	@Override
	@Transactional
	public void deleteUserRole(String roleId,String userIds) {
		if(StringUtils.isNotBlank(userIds)){
			String uids[] = userIds.split(",");
			for (String userId : uids) {
				dao.deleteUserRole(roleId,userId);
			}
		}
		
	}

	
}
